﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using CommonLibrary.DataModel.Basic;
using ServiceApplication.Service.Basic;
using ServiceApplication.Service.WorkFlow;
using System.Data;
using System.Text;
using CommonLibrary.DataModel.WorkFlow;
using CommonLibrary.DataModel.Form;
using ServiceApplication.Service;
using Bukovics.Workflow.Hosting;
using System.Workflow.Activities;
using CommonLibrary.Wf.InterFace;
using System.Xml;
using System.IO;
using CommonLibrary.Common;
using OAclient.Common;
using System.Xml.Linq;
using OAclient.Models;
using System.Net.Mail;
using ServiceApplication.Service.Plan;
using ValueMail;
using ValueMail.Send.Infrastructure;
using System.Security.Cryptography;

namespace OAclient.WebService
{
    /// <summary>
    /// MyService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class MyService : System.Web.Services.WebService
    {
        // result：1:成功
        //             0:失败
        //             -1:出错
        //

        BasicService bs = new BasicService();
        FormService fs = new FormService();
        WorkFlowService wfs = new WorkFlowService();
        SerializeDesigner designer = new SerializeDesigner();

        [WebMethod(Description = "登陆")]
        public bool Login(string loginid, string loginpsw)
        {
            try
            {
                if (string.IsNullOrEmpty(loginid) || string.IsNullOrEmpty(loginpsw)) return false;
                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                if (null == userEntity || userEntity.user_psw != loginpsw)
                {
                    return false;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        [WebMethod(Description = "获取当前服务器的时间 返回格式:yyyy-MM-dd HH:mm:ss")]
        public string GetServerTime()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        #region 用户

        [WebMethod(Description = "获取对应用户的基本 格式：25@用户名@部门编码@部门名称,26@用户名@部门编码@部门名称, ")]
        public String GetUserInfo()
        {
            try
            {
                var users = bs.QueryAllUsers();
                var userBuilder = new StringBuilder();
                if (users != null)
                {
                    var rowCount = users.Rows.Count;
                    for (int index = 0; index < rowCount; index++)
                    {
                        userBuilder.Append(users.Rows[index]["用户编码"] + "@");
                        userBuilder.Append(users.Rows[index]["用户名称"] + "@");
                        userBuilder.Append(users.Rows[index]["部门编码"] + "@");
                        userBuilder.Append(users.Rows[index]["部门名称"] + " ,");
                    }
                }
                return userBuilder.ToString();
            }
            catch
            {
                return "false";
            }
        }

        [WebMethod(Description = "获取对应用户的基本 格式：25@用户名@用户名称@员工编码@创建时间@账号@密码@状态@上次登陆时间@权限编码@审批权限编码@头像@职位编码@职位名称@部门编码@部门名称, ")]
        public String GetUserInfo2()
        {
            try
            {
                var users = bs.QueryAllUsers();
                var userBuilder = new StringBuilder();
                if (users != null)
                {
                    var rowCount = users.Rows.Count;
                    for (int index = 0; index < rowCount; index++)
                    {
                        userBuilder.Append(users.Rows[index]["用户编码"] + "@");
                        userBuilder.Append(users.Rows[index]["用户名称"] + "@");
                        userBuilder.Append(users.Rows[index]["员工编码"] + "@");
                        userBuilder.Append(users.Rows[index]["创建时间"] + "@");
                        userBuilder.Append(users.Rows[index]["账号"] + "@");
                        userBuilder.Append(users.Rows[index]["密码"] + "@");
                        userBuilder.Append(users.Rows[index]["状态"] + "@");
                        userBuilder.Append(users.Rows[index]["上次登陆时间"] + "@");
                        userBuilder.Append(users.Rows[index]["权限编码"] + "@");
                        userBuilder.Append(users.Rows[index]["审批权限编码"] + "@");
                        userBuilder.Append(users.Rows[index]["头像"] + "@");
                        userBuilder.Append(users.Rows[index]["职位编码"] + "@");
                        userBuilder.Append(users.Rows[index]["职位名称"] + "@");
                        userBuilder.Append(users.Rows[index]["部门编码"] + "@");
                        userBuilder.Append(users.Rows[index]["部门名称"] + " ,");
                    }
                }
                return userBuilder.ToString();
            }
            catch
            {
                return "false";
            }
        }

        [WebMethod(Description = "用户权限编号@用户权限@部门名称@职位名称@审批权限@员工id@创建日期@用户id@用户头像@上次登录时间@账号@姓名@密码@状态")]
        public String GetUserInfoById(String userid)
        {
            try
            {
                var user = bs.QueryUserByUserId(userid);
                var userBuilder = new StringBuilder();
                if (user != null)
                {
                    userBuilder.Append(user.auth_id + "@");
                    userBuilder.Append(user.authority + "@");
                    userBuilder.Append(user.dept_name + "@");
                    userBuilder.Append(user.duty_name + "@");
                    userBuilder.Append(user.spqx_id + "@");
                    userBuilder.Append(user.staff_id + "@");
                    userBuilder.Append(user.user_ctime + "@");
                    userBuilder.Append(user.user_id + "@");
                    userBuilder.Append(user.user_img + "@");
                    userBuilder.Append(user.user_lasttime + "@");
                    userBuilder.Append(user.user_loginid + "@");
                    userBuilder.Append(user.user_name + "@");
                    userBuilder.Append(user.user_psw + "@");
                    userBuilder.Append(user.user_status);
                }
                return userBuilder.ToString();
            }
            catch
            {
                return "false";
            }
        }

        [WebMethod(Description = "用户权限编号@用户权限@部门名称@职位名称@审批权限@员工id@创建日期@用户id@用户头像@上次登录时间@账号@姓名@密码@状态")]
        public String GetUserInfoByLoginId(String loginid, String loginpsw)
        {
            try
            {
                if (string.IsNullOrEmpty(loginid) || string.IsNullOrEmpty(loginpsw)) return "";
                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                if (null == userEntity || userEntity.user_psw != loginpsw)
                {
                    return "false";
                }
                var user = bs.QueryUserByUserId(userEntity.user_id.ToString());
                var userBuilder = new StringBuilder();
                if (user != null)
                {
                    userBuilder.Append(user.auth_id + "@");
                    userBuilder.Append(user.authority + "@");
                    userBuilder.Append(user.dept_name + "@");
                    userBuilder.Append(user.duty_name + "@");
                    userBuilder.Append(user.spqx_id + "@");
                    userBuilder.Append(user.staff_id + "@");
                    userBuilder.Append(user.user_ctime + "@");
                    userBuilder.Append(user.user_id + "@");
                    userBuilder.Append(user.user_img + "@");
                    userBuilder.Append(user.user_lasttime + "@");
                    userBuilder.Append(user.user_loginid + "@");
                    userBuilder.Append(user.user_name + "@");
                    userBuilder.Append(user.user_psw + "@");
                    userBuilder.Append(user.user_status);
                }
                return userBuilder.ToString();
            }
            catch
            {
                return "false";
            }
        }

        #endregion

        #region 流程审批


        [WebMethod(Description = @"获取历史申请列表,返回格式：任务编码@申请单名称@申请人@流程状态@已读@审批到达时间@当前处理人，/r/d
                                                   clrid:用户id  wfname:列名-名称  stime,etime格式:'2012-01-01'  ")]
        public string GetWFStartList(string loginid, int firstRecorder, int rowLength, string clrid, string wfname, string stime, string etime)
        {
            if (string.IsNullOrEmpty(loginid)) return "false";

            try
            {
                UserEntity user = bs.QueryUserByLoginid(loginid);
                var dt_wfstart = wfs.queryStartListByConditions(user.user_id.ToString(), clrid, wfname, stime, etime, "'已处理','未处理','开始'");
                if (dt_wfstart == null || dt_wfstart.Rows.Count == 0 || firstRecorder > dt_wfstart.Rows.Count) return "";

                dt_wfstart = dt_wfstart.AsEnumerable().Skip(firstRecorder).Take(rowLength).CopyToDataTable<DataRow>();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < dt_wfstart.Rows.Count; i++)
                {
                    sb.Append(dt_wfstart.Rows[i]["任务编码"].ToString() + "@");
                    sb.Append(dt_wfstart.Rows[i]["名称"].ToString() + "@");
                    sb.Append(dt_wfstart.Rows[i]["发起人"].ToString() + "@");
                    sb.Append(dt_wfstart.Rows[i]["结果"].ToString() + "@");
                    sb.Append(dt_wfstart.Rows[i]["已读"].ToString() + "@");
                    sb.Append(DateTime.Parse(dt_wfstart.Rows[i]["发起日期"].ToString()).ToString("yyyy-MM-dd HH:mm") + "@");
                    sb.Append(dt_wfstart.Rows[i]["处理人"].ToString() + " ,");
                }

                return sb.ToString();
            }
            catch
            {
                return "false";
            }
        }

        [WebMethod(Description = @"获取历史审批列表,返回格式：任务编码@申请单名称@申请人@流程状态@审批到达时间@审批完成时间, /r/d
                                                      clrid:用户id  wfname:列名-名称  stime,etime格式:'2012-01-01'  ")]
        public string GetWFFinishList(string loginid, int firstRecorder, int rowLength, string clrid, string wfname, string stime, string etime)
        {
            if (string.IsNullOrEmpty(loginid)) return "false";
            try
            {
                UserEntity user = bs.QueryUserByLoginid(loginid);
                var dt_wfworked = wfs.QueryTaskFinish(user.user_id.ToString(), clrid, wfname, stime, etime, "已处理");
                if (dt_wfworked == null || dt_wfworked.Rows.Count == 0 || firstRecorder > dt_wfworked.Rows.Count) return "";

                dt_wfworked = dt_wfworked.AsEnumerable().Skip(firstRecorder).Take(rowLength).CopyToDataTable<DataRow>();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < dt_wfworked.Rows.Count; i++)
                {
                    sb.Append(dt_wfworked.Rows[i]["任务编码"].ToString() + "@");
                    sb.Append(dt_wfworked.Rows[i]["流程名称"].ToString() + "@");
                    sb.Append(dt_wfworked.Rows[i]["发起人"].ToString() + "@");
                    sb.Append(dt_wfworked.Rows[i]["任务名称"].ToString() + "@");
                    sb.Append(DateTime.Parse(dt_wfworked.Rows[i]["开始时间"].ToString()).ToString("yyyy-MM-dd HH:mm") + "@");
                    sb.Append(DateTime.Parse(dt_wfworked.Rows[i]["完成时间"].ToString()).ToString("yyyy-MM-dd HH:mm") + " ,");
                }

                return sb.ToString();
            }
            catch
            {
                return "false";
            }
        }

        [WebMethod(Description = @"获取当前待处理任务,返回格式：任务编码@任务名称@任务状态@开始时间@发起人, /r/d
                                                             clrid:用户id  wfname:列名-名称  stime,etime格式:'2012-01-01'  ")]
        public string GetCurTaskList(string loginid, int firstRecorder, int rowLength, string clrid, string wfname, string stime, string etime)
        {
            try
            {
                if (string.IsNullOrEmpty(loginid)) return "false";

                StringBuilder sb = new StringBuilder();
                UserEntity user = bs.QueryUserByLoginid(loginid);
                DataTable dt = wfs.QueryTaskByCondition(user.user_id.ToString(), clrid, wfname, stime, etime, "未处理");
                if (dt == null || dt.Rows.Count == 0 || firstRecorder > dt.Rows.Count) return "";

                dt = dt.AsEnumerable().Skip(firstRecorder).Take(rowLength).CopyToDataTable<DataRow>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sb.Append(dt.Rows[i]["任务编码"].ToString() + "@");
                    sb.Append(dt.Rows[i]["流程名称"].ToString() + "@");
                    sb.Append(dt.Rows[i]["任务状态"].ToString() + "@");
                    sb.Append(dt.Rows[i]["开始时间"].ToString() + "@");
                    sb.Append(dt.Rows[i]["发起人"].ToString() + " ,");
                }

                return sb.ToString();
            }
            catch
            {
                return "false";
            }
        }

        //同意@指定人同意，取消@指定人取消，等待@指定人，加签@指定人加签
        [WebMethod(Description = "获取下一步的处理方式 返回格式：btn.text@btn.value,")]
        public string GetNextStep(string task_id)
        {
            if (string.IsNullOrEmpty(task_id)) return "false";

            StringBuilder sb = new StringBuilder();
            WfTaskEntity taskEntity = wfs.QueryTask(task_id.ToString());
            WfInsEntity wfinsEnitity = wfs.queryInsEntity(taskEntity.uniqueid.ToString());
            WfEntity wfEntity = wfs.QueryWf(wfinsEnitity.wf_id.ToString());
            var rtime = (System.Web.HttpContext.Current.Application[MyWorkHelpr.WorkflowRuntimeManager] as WorkflowRuntimeManager).WorkflowRuntime;
            if (null == taskEntity || wfinsEnitity == null || wfEntity == null || null == rtime)
            {
                return "false";
            }

            try
            {
                var platelist = System.Web.HttpContext.Current.Application[MyWorkHelpr.WorkflowStaticTemplate] as Dictionary<int, StateMachineWorkflowInstance>;

                DataTable dt = wfs.getNextStep(platelist[wfEntity.wftemplate_id], taskEntity.taskname);
                foreach (DataRow item in dt.Rows)
                {
                    //StepList.Add(new SelectListItem() { Text = item["NTEXT"].ToString(), Value = item["NID"].ToString(), Selected = false });
                    sb.Append(item["NTEXT"].ToString() + "@");
                    sb.Append(item["NID"].ToString() + " ,");
                }

                return sb.ToString();
            }
            catch (Exception e)
            {
                return "false";
            }

        }

        //默认是在 点击通过按钮 时调用
        // "1@张三,2@李四,"
        [WebMethod(Description = "获取不同操作方式对应的处理人 [返回为空说明不用处理]  参数：processvalue=btn.text 格式:编码@名称,")]
        public string GetClrList(string task_id, string processvalue)
        {
            if (string.IsNullOrEmpty(task_id) || string.IsNullOrEmpty(processvalue)) return "false";

            try
            {
                //取消等操作就不用返回数组
                if (GetTaskState(task_id))
                {
                    return "";//返回空，最后doProcess 方法会最后断定该任务是否已完成
                }
                var wfrManger = System.Web.HttpContext.Current.Application[MyWorkHelpr.WorkflowRuntimeManager] as WorkflowRuntimeManager;
                WfTaskEntity taskEntity = wfs.QueryTask(task_id);
                var wfinsEntity = wfs.queryInsEntity(taskEntity.uniqueid.ToString());
                var fqrEntity = bs.QueryUser(wfinsEntity.user_id);
                //StateMachineWorkflowInstance stateInstance = new StateMachineWorkflowInstance(
                //wfrManger.WorkflowRuntime, taskEntity.uniqueid);
                //EventDrivenActivity edact = (EventDrivenActivity)stateInstance.CurrentState.Activities[processvalue];
                //if (null == edact)
                //    return "false";
                //EventDrivenActivity edact = null;
                //foreach (EventDrivenActivity item in stateInstance.CurrentState.Activities)
                //{
                //    if (item.Description == processvalue)
                //        edact = (EventDrivenActivity)item;
                //}


                var platelist = System.Web.HttpContext.Current.Application[MyWorkHelpr.WorkflowStaticTemplate] as Dictionary<int, StateMachineWorkflowInstance>;
                WfEntity wfEntity = wfs.QueryWfByuniqueid(taskEntity.uniqueid.ToString());
                var task_refuse = wfs.taskRefuse(platelist, wfEntity.wftemplate_id, taskEntity.taskname, processvalue);
                if (task_refuse)
                {
                    return string.Empty;
                }
                //WfClrxxInfo clrxx = wfs.getNextClrxxByTaskName(platelist, wfEntity.wftemplate_id, edact.Parent.Name, edact.Description);
                WfClrxxInfo clrxx = wfs.getNextClrxxByTaskName(platelist, wfEntity.wftemplate_id, taskEntity.taskname, processvalue);
                var se = bs.QueryStaff(fqrEntity.staff_id);

                if (clrxx != null && clrxx.clzType == TaskClrType.按权限.ToString())
                {
                    string[] wfspqxnames = clrxx.AsQx;
                    var dt_qx = wfs.WfSpqxQuery(wfspqxnames);
                    var arr_qxid = dt_qx.AsEnumerable().Select(s => s.Field<int>("编码")).ToArray();
                    DataTable dt_clr = null;

                    if (!clrxx.IsSelfDept)
                        dt_clr = bs.QueryUserBySpqx(arr_qxid);
                    else
                        dt_clr = bs.QueryUserBySpqxDept(arr_qxid, se.dept_id);

                    //按职务的类型下如果有多人，则多人抢占式发起
                    if (dt_clr != null && dt_clr.Rows.Count == 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        DataTable dt = bs.QueryAllUsers();
                        foreach (DataRow dr in dt.Rows)
                        {
                            sb.Append(dr["用户编码"].ToString() + "@");
                            sb.Append(dr["用户名称"].ToString() + " ,");
                        }
                        return sb.ToString();
                    }
                }
                else if (clrxx != null && clrxx.clzType == TaskClrType.由上一步处理人指定.ToString())
                {
                    StringBuilder sb = new StringBuilder();
                    DataTable dt = bs.QueryAllUsers();
                    foreach (DataRow dr in dt.Rows)
                    {
                        sb.Append(dr["用户编码"].ToString() + "@");
                        sb.Append(dr["用户名称"].ToString() + " ,");
                    }
                    return sb.ToString();
                }

                return string.Empty;

            }
            catch
            {
                return "false";
            }

        }

        [WebMethod(Description = "审批执行 [返回空说明已处理] processname:btn.text,processvalue:btn.value,clrid='' 即不是指定人")]
        public string DoProcess(string loginid, string task_id, string processname, string processvalue, string processclyj, string clrids)
        {
            if (string.IsNullOrEmpty(loginid) || string.IsNullOrEmpty(task_id) || string.IsNullOrEmpty(processname) || string.IsNullOrEmpty(processvalue))
                return "false";
            try
            {
                //先判断是否已经处理
                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                WfTaskEntity taskEntity = wfs.QueryTask(task_id);
                //如果已经处理就直接返回true
                if (taskEntity.state != "未处理")
                    return "";

                int[] clrs = null;
                if (!string.IsNullOrEmpty(clrids))
                {
                    //clrs = new int[] { int.Parse(clrids) };
                    string[] selectarr = clrids.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    clrs = Array.ConvertAll<string, int>(selectarr, delegate(string d) { return int.Parse(d); });
                }

                #region 处理操作

                FormEntity formEntity = fs.QueryFormEntityByWfinsId(taskEntity.uniqueid.ToString());
                Guid instanceId = taskEntity.uniqueid;

                List<FormFieldEntity> ffeList = fs.QueryFormFieldEntitys(formEntity.form_id, "'text'");

                List<WfInsvalueEntity> wivList = wfs.QueryInsvalue(taskEntity.ptask_id.ToString());//用上一步的数据作为该任务的数据
                wivList.ForEach(x => x.task_id = int.Parse(task_id));
                if (null == ffeList || null == wivList)
                    return "false";

                WorkflowRuntimeManager manager = (System.Web.HttpContext.Current.Application[MyWorkHelpr.WorkflowRuntimeManager] as WorkflowRuntimeManager);
                {
                    var resTag = wfs.DoWfProcess2(manager, instanceId, userEntity, task_id.ToString(), taskEntity.ptask_id.ToString(), processvalue, processname, processclyj, wivList, clrs,
                         (System.Web.HttpContext.Current.Application[MyWorkHelpr.WorkflowStaticTemplate] as Dictionary<int, StateMachineWorkflowInstance>), "");
                    if (resTag.res)
                        return "true";
                    else
                        return "false";
                }

                #endregion
            }
            catch (Exception e)
            {
                return "false";
            }
        }

        [WebMethod(Description = "获取对应task的状态 true:已完成；false:未处理")]
        public bool GetTaskState(string task_id)
        {
            if (string.IsNullOrEmpty(task_id)) return false;
            WfTaskEntity taskEntity = wfs.QueryTask(task_id.ToString());
            if (taskEntity.state == "未处理")
                return false;
            else
                return true;
        }

        //[WebMethod(Description = "新建申请单 格式:{\"财务费用\":[{\"id\":\"表单编号\",\"name\":\"表单名称\",\"time\":\"创建时间\",\"creator\"\"创建人\"}]}")]
        //public String GetWFList()
        //{
        //    try
        //    {
        //        var dt_wftype = wfs.QueryWftypeAll();
        //        var dt_wf = wfs.QueryAllWfByState(1);
        //        var wftypCount = dt_wftype.Rows.Count;
        //        var result = "{";
        //        for (int wftypeIndex = 0; wftypeIndex < wftypCount; wftypeIndex++)
        //        {
        //            result += "\"" + dt_wftype.Rows[wftypeIndex]["名称"] + "\":[";
        //            var sldt_wf = dt_wf.Select("类型编码='" + dt_wftype.Rows[wftypeIndex]["编码"].ToString() + "'");
        //            var wfCount = sldt_wf.Count();
        //            for (int wfIndex = 0; wfIndex < wfCount; wfIndex++)
        //            {
        //                result += "{\"id\":\"" + dt_wf.Rows[wfIndex]["编码"] + "\",";
        //                result += "\"name\":\"" + dt_wf.Rows[wfIndex]["名称"] + "\",";
        //                result += "\"time\":\"" + dt_wf.Rows[wfIndex]["创建时间"] + "\",";
        //                result += "\"crestor\":\"" + dt_wf.Rows[wfIndex]["创建人"] + "\"}";
        //                if (wfIndex != wfCount - 1)
        //                    result += ",";
        //            }
        //            result += "]";
        //            if (wftypeIndex != wftypCount - 1)
        //                result += ",";
        //        }
        //        result += "}";
        //        return result;
        //    }
        //    catch
        //    {
        //        return "";
        //    }
        //}

        //public string WFStart(string wf_id, string user_id)
        //{
        //    if (string.IsNullOrEmpty(wf_id) || string.IsNullOrEmpty(user_id))
        //        return "false";

        //    UserEntity user = bs.QueryUser(int.Parse(user_id));
        //    StaffEntity se = bs.QueryStaff(user.staff_id);
        //    DataTable dt_deptuser = bs.QueryUserByDeptId(se.dept_id.ToString());
        //    //获取本部门人员列表
        //    string[] arr_deptuser = dt_deptuser.AsEnumerable().Select(r => r["用户名称"].ToString()).ToArray();
        //    DeptEntity dept = bs.QueryDept(se.dept_id);
        //    DutyEntity duty = bs.QueryDuty(se.duty_id);
        //    WfEntity wfEntity = wfs.QueryWf(wf_id);
        //    FormEntity formEntity = fs.QueryFormEntity(wfEntity.form_id);

        //    if (formEntity == null)
        //        return "false";

        //    XElement xml = XElement.Load(Server.MapPath("~/UploadFiles/Form/" + formEntity.location));
        //    WorkFlowModel wfm = new WorkFlowModel();
        //    //ViewData["formdefi"] = designer.DeserializeXmlToWeb(xml, new SerializeAutoData(true) { user_name = user.user_name, user_deptname = dept.dept_name, user_dutyname = duty.duty_name, user_id = user.user_id.ToString(), ComboList = arr_deptuser });
        //    //ViewData["form_id"] = formEntity.form_id.ToString();
        //    //ViewData["wftemplate_id"] = wfEntity.wftemplate_id.ToString();
        //    //ViewData["wf_id"] = wfEntity.wf_id.ToString();
        //}

        #region 委托处理
        [WebMethod(Description = "，返回false说明不存在有效委托")]
        public bool WFDelegCancel(string loginid)
        {
            UserEntity userEntity = bs.QueryUserByLoginid(loginid);
            WfDelegEntity wfDelegEntity = wfs.QueryWfDelegCur(userEntity.user_id);
            if (wfDelegEntity != null)
            {
                wfDelegEntity.deleg_status = "0";
                wfDelegEntity.deleg_etime = DateUtil.getStandardTimeBySecond();
                wfDelegEntity.deleg_closer = userEntity.user_id;
                wfs.UpdateWfDeleg(wfDelegEntity);
                return true;
            }
            return false;
        }

        [WebMethod(Description = "deleg_str_id 受托人的编号 返回false说明已存在有效委托，应该先取消委托，然后再委托操作")]
        public bool WFDelegAdd(string loginid, string deleg_str_id)
        {
            UserEntity userEntity = bs.QueryUserByLoginid(loginid);
            //后台验证是否已经存在有效委托
            WfDelegEntity tmp = wfs.QueryWfDelegCur(userEntity.user_id);
            if (tmp != null)
                return false;

            WfDelegEntity wfDelegEntity = new WfDelegEntity()
            {
                deleg_status = "1",
                deleg_accept = "未处理",
                deleg_str = int.Parse(deleg_str_id),
                deleg_wtr = userEntity.user_id,
                deleg_stime = DateUtil.getStandardTimeBySecond(),
                deleg_starter = userEntity.user_id,
                deleg_closer = -1,
                deleg_etime = "",
                deleg_rectime = "",
                deleg_descn = "",
            };
            wfs.AddWfDeleg(wfDelegEntity);

            return true;
        }

        [WebMethod(Description = "返回false字符串说明查询没有委托，否则返回受托人名字   ")]
        public String WFDelegGetCurStr(string loginid)
        {
            UserEntity userEntity = bs.QueryUserByLoginid(loginid);
            //后台验证是否已经存在有效委托
            WfDelegEntity tmp = wfs.QueryWfDelegCur(userEntity.user_id);
            if (null == tmp)
                return "false";
            else
            {
                UserEntity ue = bs.QueryUser(tmp.deleg_str);
                if (ue == null) return "false";
                return ue.user_name;
            }
        }
        #endregion

        #endregion

        #region 流程审批 返回json str

        //[WebMethod(Description = "{\"id\":[\"mc\"],\"id\":[\"mc\"]}")]
        //public String GetApprovalList()
        //{
        //    try
        //    {
        //        var wflist = wfs.QueryAllWf();
        //        var result = "{";
        //        var rowCount = 0;
        //        if (wflist != null)
        //            rowCount = wflist.Rows.Count;
        //        if (rowCount == 0) return "";
        //        for (int rowIndex = 0; rowIndex < rowCount - 1; rowIndex++)
        //        {
        //            result += "\"" + wflist.Rows[rowIndex]["编码"] + "\":[\"" + wflist.Rows[rowIndex]["名称"] + "\"],";
        //        }
        //        result += "\"" + wflist.Rows[rowCount - 1]["编码"] + "\":[\"" + wflist.Rows[rowCount - 1]["名称"] + "\"]}";
        //        return result;
        //    }
        //    catch
        //    {
        //        return "";
        //    }
        //}

        //[WebMethod(Description = "{\"handler\":[{\"username\":\"用户名称\",\"deptname\":\"部门名称\",\"postname\":\"职位名称\",\"userid\":\"用户编码\"},]}")]
        //public String GetHandler()
        //{
        //    try
        //    {
        //        var dt = bs.QueryAllUsers();

        //        var result = "{\"handler\":[";
        //        var rowCount = 0;
        //        if (dt != null)
        //            rowCount = dt.Rows.Count;
        //        if (rowCount == 0) return "";
        //        for (int rowIndex = 0; rowIndex < rowCount - 1; rowIndex++)
        //        {
        //            result += "{ \"username\":\"" + dt.Rows[rowIndex]["用户名称"] + "\", \"deptname\": \"" + dt.Rows[rowIndex]["部门名称"]
        //                + "\",\"postname\":\"" + dt.Rows[rowIndex]["职位名称"] + "\",\"userid\":\"" + dt.Rows[rowIndex]["用户编码"] + "\"},";
        //        }

        //        result += "{\" username\":\"" + dt.Rows[rowCount - 1]["用户名称"] + "\", \"deptname\": " + dt.Rows[rowCount - 1]["部门名称"]
        //            + "\",\"postname\":\"" + dt.Rows[rowCount - 1]["职位名称"] + "\",\"userid\":\"" + dt.Rows[rowCount - 1]["用户编码"] + "\"]}";
        //        return result;
        //    }
        //    catch
        //    {
        //        return "";
        //    }
        //}

        #endregion

        #region 通讯录

        //[WebMethod(Description = "获取对应用户的通讯录列表 根级节点 type:wd,gx,gs 格式：25@我的好友@,26@同事@,27@亲戚, ")]
        //public string GetTxlGroup(string loginid, string type)
        //{
        //    #region 参数验证

        //    if (string.IsNullOrEmpty(loginid) || string.IsNullOrEmpty(type)) return "false";
        //    if (type != "wd" && type != "gx" && type != "gs") return "false";

        //    #endregion

        //    try
        //    {
        //        StringBuilder res = new StringBuilder();
        //        if (type == "wd")
        //        {
        //            UserEntity userEntity = bs.QueryUserByLoginid(loginid);
        //            if (userEntity == null) return "false";
        //            var data = bs.QueryTxlGroupByUserId(userEntity.user_id);
        //            if (data == null || data.Rows.Count == 0) return string.Empty;

        //            for (int i = 0; i < data.Rows.Count; i++)
        //            {
        //                res.Append(data.Rows[i]["小组编号"].ToString() + "@");
        //                res.Append(data.Rows[i]["名称"].ToString() + "@");
        //                res.Append(data.Rows[i]["备注"].ToString() + " ,");
        //            }
        //        }
        //        if (type == "gx")
        //        {
        //            var data = bs.QueryTxlGroupByUserId(1);//这里默认管理员编号是 1 待改进
        //            if (data == null || data.Rows.Count == 0) return string.Empty;

        //            for (int i = 0; i < data.Rows.Count; i++)
        //            {
        //                res.Append(data.Rows[i]["小组编号"].ToString() + "@");
        //                res.Append(data.Rows[i]["名称"].ToString() + "@");
        //                res.Append(data.Rows[i]["备注"].ToString() + " ,");
        //            }
        //        }
        //        if (type == "gs")
        //        {
        //            var data = bs.QueryAllDepts();//这里默认管理员编号是 1 待改进
        //            if (data == null || data.Rows.Count == 0) return string.Empty;

        //            for (int i = 0; i < data.Rows.Count; i++)
        //            {
        //                res.Append(data.Rows[i]["部门编码"].ToString() + "@");
        //                res.Append(data.Rows[i]["部门名称"].ToString() + "@");
        //                res.Append("无" + " ,");
        //            }
        //        }
        //        return res.ToString();
        //    }
        //    catch
        //    {
        //        return "false";
        //    }
        //}

        [WebMethod(Description = "获取对应用户的通讯录列表 根级节点 格式：人员id@人员名ψ小组id@小组名@wd,人员id@人员名ψ小组id@小组名@gx, ")]
        public string GetAllTxl(string loginid)
        {
            if (string.IsNullOrEmpty(loginid)) return "false";

            try
            {
                StringBuilder res = new StringBuilder();

                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                if (userEntity == null) return "false";
                var data = bs.QueryTxlByUserId(userEntity.user_id);
                if (data != null && data.Rows.Count > 0)
                {
                    for (int i = 0; i < data.Rows.Count; i++)
                    {
                        res.Append(data.Rows[i]["编号"].ToString() + "@");
                        res.Append(data.Rows[i]["名称"].ToString() + "ψ");
                        res.Append(data.Rows[i]["小组编号"].ToString() + "@");
                        res.Append(data.Rows[i]["小组名称"].ToString() + "@");
                        res.Append("wd ,");
                    }
                }

                var data2 = bs.QueryTxlByUserId(1);//这里默认管理员编号是 1 待改进
                if (data2 != null && data2.Rows.Count > 0)
                {
                    for (int i = 0; i < data2.Rows.Count; i++)
                    {
                        res.Append(data2.Rows[i]["编号"].ToString() + "@");
                        res.Append(data2.Rows[i]["名称"].ToString() + "ψ");
                        res.Append(data2.Rows[i]["小组编号"].ToString() + "@");
                        res.Append(data2.Rows[i]["小组名称"].ToString() + "@");
                        res.Append("gx ,");
                    }
                }

                var data3 = bs.QueryAllStaff();
                if (data3 != null && data3.Rows.Count > 0)
                {
                    for (int i = 0; i < data3.Rows.Count; i++)
                    {
                        res.Append(data3.Rows[i]["员工编码"].ToString() + "@");
                        res.Append(data3.Rows[i]["员工名称"].ToString() + "ψ");
                        res.Append(data3.Rows[i]["部门编码"].ToString() + "@");
                        res.Append(data3.Rows[i]["部门名称"].ToString() + "@");
                        res.Append("gs ,");
                    }
                }

                return res.ToString();
            }
            catch
            {
                return "false";
            }
        }

        [WebMethod(Description = "获取对应用户的通讯录列表 根级节点 格式：12ψ张三ψ手机ψ短号ψ座机ψ地址ψEmailψQQψfaxψ部门ψ职位ψ备注, ")]
        public string GetAllTxlDetial(string loginid)
        {
            if (string.IsNullOrEmpty(loginid)) return "false";
            UserEntity userEntity = bs.QueryUserByLoginid(loginid);
            if (userEntity == null) return "false";
            StringBuilder res = new StringBuilder();
            try
            {
                var dt_txl = bs.QueryAllTxl();
                if (dt_txl != null && dt_txl.Rows.Count > 0)
                {
                    for (int i = 0; i < dt_txl.Rows.Count; i++)
                    {
                        res.Append(dt_txl.Rows[i]["编号"].ToString() + "ψ");
                        res.Append(dt_txl.Rows[i]["名称"].ToString() + "ψ");
                        res.Append(dt_txl.Rows[i]["手机"].ToString() + "ψ");
                        res.Append(" " + "ψ");
                        res.Append(dt_txl.Rows[i]["固定电话"].ToString() + "ψ");
                        res.Append(dt_txl.Rows[i]["联系地址"].ToString() + "ψ");
                        res.Append(dt_txl.Rows[i]["Email"].ToString() + "ψ");
                        res.Append(dt_txl.Rows[i]["QQ"].ToString() + "ψ");
                        res.Append(dt_txl.Rows[i]["传真"].ToString() + "ψ");
                        res.Append(dt_txl.Rows[i]["部门"].ToString() + "ψ");
                        res.Append(dt_txl.Rows[i]["职位"].ToString() + "ψ");
                        res.Append(dt_txl.Rows[i]["备注"].ToString() + " ,");

                    }
                }
            }
            catch
            {
                return "false";
            }
            return res.ToString();
        }

        [WebMethod(Description = "获取公司员工的通讯录列表 根级节点 格式：12ψ张三ψ手机ψ短号ψ座机ψ地址ψEmailψQQψfaxψ部门ψ职位ψ备注, ")]
        public string GetAllTxlGsDetial()
        {
            try
            {
                var staffs = bs.QueryAllStaff();
                var res = new StringBuilder();
                if (staffs != null)
                {
                    var rowCount = staffs.Rows.Count;
                    for (int index = 0; index < rowCount; index++)
                    {
                        res.Append(staffs.Rows[index]["员工编码"] + "ψ");
                        res.Append(staffs.Rows[index]["员工名称"] + "ψ");
                        res.Append(staffs.Rows[index]["手机"] + "ψ");
                        res.Append(staffs.Rows[index]["短号"] + "ψ");
                        res.Append(staffs.Rows[index]["办公电话"] + "ψ");
                        res.Append(staffs.Rows[index]["现居地址"] + "ψ");
                        res.Append(staffs.Rows[index]["邮箱"] + "ψ");
                        res.Append(staffs.Rows[index]["qq"] + "ψ");
                        res.Append(staffs.Rows[index]["传真"] + "ψ");
                        res.Append(staffs.Rows[index]["部门名称"] + "ψ");
                        res.Append(staffs.Rows[index]["职位名称"] + "ψ");
                        res.Append(staffs.Rows[index]["其它"] + " ,");
                    }
                }
                return res.ToString();
            }
            catch
            {
                return "false";
            }
        }

        [WebMethod(Description = "获取对应用户的通讯录列表|  type:wd,gx,gs | 返回格式：12@张三,13@李四,14@王五, ")]
        public string GetTxlByGroupId(string txlgroup_id, string type)
        {
            #region 参数验证

            if (string.IsNullOrEmpty(txlgroup_id) || string.IsNullOrEmpty(type)) return "false";
            if (type != "wd" && type != "gx" && type != "gs") return "false";

            #endregion

            try
            {

                StringBuilder res = new StringBuilder();
                if (type == "wd" || type == "gx")
                {
                    var data = bs.QueryTxlByTxlGroupId(int.Parse(txlgroup_id));
                    if (data == null || data.Rows.Count == 0) return string.Empty;

                    for (int i = 0; i < data.Rows.Count; i++)
                    {
                        res.Append(data.Rows[i]["编号"].ToString() + "@");
                        res.Append(data.Rows[i]["名称"].ToString() + " ,");
                    }
                    return res.ToString();
                }
                if (type == "gs")
                {
                    var data = bs.QueryUserByDeptId(txlgroup_id);
                    if (data == null || data.Rows.Count == 0) return string.Empty;

                    for (int i = 0; i < data.Rows.Count; i++)
                    {
                        res.Append(data.Rows[i]["员工编码"].ToString() + "@");
                        res.Append(data.Rows[i]["员工名称"].ToString() + " ,");
                    }
                    return res.ToString();
                }
            }
            catch
            {
                return "false";
            }
            return "false";
        }

        [WebMethod(Description = "获取莫个通讯单位的详细信息 type:wd,gx,gs 返回格式：12ψ张三ψ手机ψ座机ψ地址ψEmailψQQψfaxψ部门ψ职位ψ备注 ")]
        public string GetTxlByTxlId(string txl_id, string type)
        {
            #region 参数验证

            if (string.IsNullOrEmpty(txl_id) || string.IsNullOrEmpty(type)) return "false";
            if (type != "wd" && type != "gx" && type != "gs") return "false";

            #endregion

            try
            {
                if (type == "wd" || type == "gx")
                {
                    TxlEntity txlEntity = bs.QueryTxl(int.Parse(txl_id));
                    if (txlEntity == null) return "false";

                    StringBuilder res = new StringBuilder();
                    //string tmp = "{ 'txl_id':'{0}','txl_name':'{1}','txl_cellphone':'{2}','txl_telephone':'{3}','txl_address':'{4}','txl_email':'{5}','txl_QQ':'{6}','txl_fax':'{7}','txl_dept':'{8}','txl_duty':'{9}','txl_descn':'{10}' }";
                    //string res = string.Format(tmp, txlEntity.txl_id, txlEntity.txl_name, txlEntity.txl_cellphone, txlEntity.txl_telephone, txlEntity.txl_address, txlEntity.txl_email, txlEntity.txl_QQ, txlEntity.txl_fax, txlEntity.txl_dept, txlEntity.txl_duty, txlEntity.txl_descn);

                    res.Append(txlEntity.txl_id + "ψ");
                    res.Append(txlEntity.txl_name + "ψ");
                    res.Append(txlEntity.txl_cellphone + "ψ");
                    res.Append(txlEntity.txl_telephone + "ψ");
                    res.Append(txlEntity.txl_address + "ψ");
                    res.Append(txlEntity.txl_email + "ψ");
                    res.Append(txlEntity.txl_QQ + "ψ");
                    res.Append(txlEntity.txl_fax + "ψ");
                    res.Append(txlEntity.txl_dept + "ψ");
                    res.Append(txlEntity.txl_duty + "ψ");
                    res.Append(txlEntity.txl_descn + " ψ");
                    return res.ToString();
                }
                if (type == "gs")
                {
                    StaffEntity staffEntity = bs.QueryStaff(int.Parse(txl_id));
                    DeptEntity deptEntity = bs.QueryDept(staffEntity.dept_id);
                    DutyEntity dutyEntity = bs.QueryDuty(staffEntity.duty_id);
                    StringBuilder res = new StringBuilder();
                    res.Append(staffEntity.staff_id + "ψ");
                    res.Append(staffEntity.staff_name + "ψ");
                    res.Append(staffEntity.staff_mobile + "ψ");
                    res.Append(staffEntity.staff_officenum + "ψ");
                    res.Append(staffEntity.staff_liveaddr + "ψ");
                    res.Append(staffEntity.staff_email + "ψ");
                    res.Append(staffEntity.staff_qq + "ψ");
                    res.Append(staffEntity.staff_fax + "ψ");
                    res.Append(deptEntity.dept_name + "ψ");
                    res.Append(dutyEntity.duty_name + "ψ");
                    res.Append(staffEntity.staff_descn + " ψ");
                    return res.ToString();
                }
            }
            catch
            {
                return "false";
            }
            return "false";
        }

        [WebMethod]
        public bool AddGroup(string loginid, string txlgroup_name)
        {
            if (string.IsNullOrEmpty(loginid) || string.IsNullOrEmpty(txlgroup_name)) return false;
            try
            {
                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                if (userEntity == null) return false;

                TxlGroupEntity txlGroupEntity = new TxlGroupEntity()
                {
                    txlgroup_descn = string.Empty,
                    txlgroup_name = txlgroup_name,
                    user_id = userEntity.user_id,
                };
                bs.AddTxlGroup(txlGroupEntity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        [WebMethod]
        public bool DelGroup(string txlgroup_id)
        {
            if (string.IsNullOrEmpty(txlgroup_id)) return false;
            try
            {
                var dt_txl = bs.QueryTxlByTxlGroupId(int.Parse(txlgroup_id));
                if (dt_txl == null || 0 != dt_txl.Rows.Count)
                    return false;

                bs.DelTxlGroup(int.Parse(txlgroup_id));

                return true;
            }
            catch
            {
                return false;
            }
        }

        [WebMethod(Description = "返回false 表示不存在该小组")]
        public bool UpdateGroup(string txlgroup_id, string txlgroup_name)
        {
            if (string.IsNullOrEmpty(txlgroup_name) || string.IsNullOrEmpty(txlgroup_id)) return false;
            try
            {
                var txlGroupEntity = bs.QueryTxlGroup(int.Parse(txlgroup_id));
                if (null == txlGroupEntity) return false;

                txlGroupEntity.txlgroup_name = txlgroup_name;
                bs.UpdateTxlGroup(txlGroupEntity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        [WebMethod]
        public string AddTxl(string txl_name, string txl_cellphone, string txl_telephone, string txl_address,
         string txl_email, string txl_QQ, string txl_fax, string txl_dept, string txl_duty, string txl_descn, string txlgroup_id, string loginid)
        {
            if (string.IsNullOrEmpty(loginid) || string.IsNullOrEmpty(txlgroup_id) || string.IsNullOrEmpty(txl_name)) return "false";
            try
            {
                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                if (userEntity == null) return "false";

                TxlEntity txlEntity = new TxlEntity()
                {
                    txl_name = txl_name,
                    txl_cellphone = txl_cellphone,
                    txl_telephone = txl_telephone,
                    txl_address = txl_address,
                    txl_email = txl_email,
                    txl_QQ = txl_QQ,
                    txl_fax = txl_fax,
                    txl_dept = txl_dept,
                    txl_duty = txl_duty,
                    txl_descn = txl_descn,
                    txlgroup_id = int.Parse(txlgroup_id),
                    user_id = userEntity.user_id,
                };
                var id = bs.AddTxl(txlEntity);
                return id.ToString();
            }
            catch (Exception e)
            {
                return "false";
            }
        }

        [WebMethod]
        public bool DelTxl(string txl_id)
        {
            if (string.IsNullOrEmpty(txl_id)) return false;
            try
            {
                bs.DelTxl(int.Parse(txl_id));
                return true;
            }
            catch
            {
                return false;
            }
        }

        [WebMethod]
        public bool UpdateTxl(string txl_id, string txl_name, string txl_cellphone, string txl_telephone, string txl_address, string txl_email,
            string txl_QQ, string txl_fax, string txl_dept, string txl_duty, string txl_descn)
        {
            if (string.IsNullOrEmpty(txl_id) || string.IsNullOrEmpty(txl_name)) return false;

            try
            {
                TxlEntity txlEntity = bs.QueryTxl(int.Parse(txl_id));
                if (txlEntity == null) return false;

                txlEntity.txl_name = txl_name;
                txlEntity.txl_cellphone = txl_cellphone;
                txlEntity.txl_telephone = txl_telephone;
                txlEntity.txl_address = txl_address;
                txlEntity.txl_email = txl_email;
                txlEntity.txl_QQ = txl_QQ;
                txlEntity.txl_fax = txl_fax;
                txlEntity.txl_dept = txl_dept;
                txlEntity.txl_duty = txl_duty;
                txlEntity.txl_descn = txl_descn;

                bs.UpdateTxl(txlEntity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region 站内信

        [WebMethod(Description = "站内信接收列表：参数:readflag:0/1/null ; msgids:在手机端本地数据库中记录的未读数据的id集合，例如 '1,2,' 返回格式：编码@标题@内容@发送时间@发起人@已读(0:未读；1:已读)@附件§ ")]
        public string MsgReceiveList(string loginid, int firstRecorder, int rowLength, string title, string content, string stime, string etime, string readflag)
        {
            #region 参数验证

            if (string.IsNullOrEmpty(loginid)) return "false";

            int? isread;
            if (string.IsNullOrEmpty(readflag)) isread = null;
            else if (readflag == "1" || readflag == "0") isread = int.Parse(readflag);
            else return "false";

            if (firstRecorder < 0) return "false";
            if (rowLength < 0) return "false";

            #endregion

            try
            {
                StringBuilder res = new StringBuilder();
                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                if (userEntity == null) return string.Empty;

                var tmp_dt = bs.QueryReceveMsgByCondition(userEntity.user_id, title, content, stime, etime, 0, isread);
                if (tmp_dt == null && tmp_dt.Rows.Count == 0 || firstRecorder > tmp_dt.Rows.Count) return "";

                tmp_dt.DefaultView.Sort = "发送时间 desc";
                DataTable data = tmp_dt.DefaultView.ToTable();
                if (data == null || data.Rows.Count == 0 || firstRecorder > data.Rows.Count) return "";

                data = data.AsEnumerable().Skip(firstRecorder).Take(rowLength).CopyToDataTable<DataRow>();
                var temp_drs = data.Select("标题 not like '企业公告%'");
                if (temp_drs.Length > 0)
                    data = temp_drs.CopyToDataTable();
                else
                    data = data.Clone();
                //将要显示的消息更新为已读
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    res.Append(data.Rows[i]["编码"].ToString() + "@");
                    res.Append(data.Rows[i]["标题"].ToString() + "@");
                    res.Append(data.Rows[i]["内容2"].ToString() + "@");
                    res.Append(data.Rows[i]["发送时间"].ToString() + "@");
                    res.Append(data.Rows[i]["发起人"].ToString() + "@");
                    res.Append(data.Rows[i]["已读"].ToString() + "@");
                    res.Append(data.Rows[i]["附件"].ToString() + " §");
                }
                return res.ToString();
            }
            catch
            {
                return "false";
            }
        }

        [WebMethod(Description = "站内信发送列表：readflag:全部已读/部分已读/未读 返回格式：编码@标题@内容@发送时间@收件人@已读(全部已读，部分已读，未读)@附件§")]
        public string MsgSendList(string loginid, int firstRecorder, int rowLength, string title, string content, string stime, string etime, string readflag)
        {

            #region 参数验证

            if (string.IsNullOrEmpty(loginid)) return "false";
            if (!string.IsNullOrEmpty(readflag) && readflag != "全部已读" && readflag != "部分已读" && readflag != "未读") return "false";

            if (firstRecorder < 0) return "false";
            if (rowLength < 0) return "false";

            #endregion

            try
            {
                StringBuilder res = new StringBuilder();
                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                if (userEntity == null) return string.Empty;

                var tmp_dt = bs.QueryMsgByCodition(userEntity.user_id, title, content, stime, etime, 0, readflag);
                var temp_drs = tmp_dt.Select("标题 not like '企业公告%'");
                if (temp_drs.Length > 0)
                    tmp_dt = temp_drs.CopyToDataTable();
                else
                    tmp_dt = tmp_dt.Clone();
                tmp_dt.DefaultView.Sort = "发送时间 desc";
                DataTable data = tmp_dt.DefaultView.ToTable();
                if (tmp_dt == null && tmp_dt.Rows.Count == 0 || firstRecorder > tmp_dt.Rows.Count) return "";

                data = data.AsEnumerable().Skip(firstRecorder).Take(rowLength).CopyToDataTable<DataRow>();
                //将要显示的消息更新为已读
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    res.Append(data.Rows[i]["编码"].ToString() + "@");
                    res.Append(data.Rows[i]["标题"].ToString() + "@");
                    res.Append(data.Rows[i]["内容2"].ToString() + "@");
                    res.Append(data.Rows[i]["发送时间"].ToString() + "@");
                    res.Append(data.Rows[i]["接收人"].ToString() + "@");
                    res.Append(data.Rows[i]["已读"].ToString() + "@");
                    res.Append(data.Rows[i]["附件"].ToString() + " §");
                }
                return res.ToString();
            }
            catch
            {
                return "false";
            }
        }

        [WebMethod(Description = "点对点获取站内信历史记录 ")]
        public string MsgList(string creater_id, string receiver_id, int firstRecorder, int rowLength, string title, string content, string stime, string etime)
        {
            if (string.IsNullOrEmpty(creater_id)) return "false";
            if (string.IsNullOrEmpty(receiver_id)) return "false";
            try
            {
                StringBuilder res = new StringBuilder();

                var tmp_dt = bs.QueryMsg(int.Parse(creater_id), int.Parse(receiver_id), title, content, stime, etime);
                var temp_drs = tmp_dt.Select("标题 not like '企业公告%'");
                if (temp_drs.Length > 0)
                    tmp_dt = temp_drs.CopyToDataTable();
                else
                    tmp_dt = tmp_dt.Clone();
                tmp_dt.DefaultView.Sort = "发送时间 desc";
                DataTable data = tmp_dt.DefaultView.ToTable();
                if (tmp_dt == null && tmp_dt.Rows.Count == 0 || firstRecorder >= tmp_dt.Rows.Count) return "";

                data = data.AsEnumerable().Skip(firstRecorder).Take(rowLength).CopyToDataTable<DataRow>();
                //将要显示的消息更新为已读
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    res.Append(data.Rows[i]["编码"].ToString() + "@");
                    res.Append(data.Rows[i]["标题"].ToString() + "@");
                    res.Append(data.Rows[i]["内容2"].ToString() + "@");
                    res.Append(data.Rows[i]["发送时间"].ToString() + "@");
                    res.Append(data.Rows[i]["接收人"].ToString() + "@");
                    res.Append(data.Rows[i]["已读"].ToString() + "@");
                    res.Append(data.Rows[i]["附件"].ToString() + " §");
                }
                return res.ToString();
            }
            catch (Exception e)
            {
                return "false";
            }
        }

        [WebMethod(Description = "站内信：将消息状态更改为已读")]
        public bool MsgRead(string msg_id, string loginid)
        {
            #region 参数验证

            if (string.IsNullOrEmpty(msg_id) || string.IsNullOrEmpty(loginid)) return false;

            #endregion

            UserEntity userEntity = bs.QueryUserByLoginid(loginid);
            if (userEntity == null) return false;

            try
            {
                bs.DoMsgRead(int.Parse(msg_id), userEntity.user_id);
                return true;
            }
            catch
            {
                return false;
            }
        }

        [WebMethod(Description = "站内信：将消息状态更改为已读 参数：msg_ids:'1,2,' ")]
        public bool DoMsgRead(string msg_ids, string loginid)
        {
            if (string.IsNullOrEmpty(msg_ids)) return false;

            UserEntity userEntity = bs.QueryUserByLoginid(loginid);
            if (userEntity == null) return false;
            try
            {
                var arr_msg_id = msg_ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < arr_msg_id.Length; i++)
                {
                    bs.DoMsgRead(int.Parse(arr_msg_id[i]), userEntity.user_id);
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        [WebMethod(Description = "站内信明细：根据msg_id获取对应接收人们的已读状态 返回格式：明细编码@接收人编码@接收人名称@已读标志(0/1) §")]
        public string MsgMx(string msg_id)
        {
            StringBuilder res = new StringBuilder();
            var data = bs.QueryMsgmx(int.Parse(msg_id));
            for (int i = 0; i < data.Rows.Count; i++)
            {
                res.Append(data.Rows[i]["消息明细编码"].ToString() + "@");
                res.Append(data.Rows[i]["接收人编码"].ToString() + "@");
                res.Append(data.Rows[i]["接收人"].ToString() + "@");
                res.Append(data.Rows[i]["已读标志"].ToString() + " §");
            }
            return res.ToString();
        }

        [WebMethod(Description = "站内信：人员选择列表    格式： 编码@姓名,编码2@姓名2,")]
        public string MsgReceiversList()
        {
            try
            {
                StringBuilder res = new StringBuilder();
                var data = bs.QueryAllUsers();
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    res.Append(data.Rows[i]["用户编码"].ToString() + "@");
                    res.Append(data.Rows[i]["用户名称"].ToString() + ",");
                }
                return res.ToString();
            }
            catch
            {
                return "false";
            }
        }

        [WebMethod(Description = "站内信：发送消息 msg_content!=''   receiver_ids='1,2,'   ")]
        public string MsgSend(string msg_title, string msg_content, string receiver_ids, string loginid)
        {
            if (string.IsNullOrEmpty(msg_title) || string.IsNullOrEmpty(msg_content) || string.IsNullOrEmpty(receiver_ids) || string.IsNullOrEmpty(loginid))
                return "false";

            try
            {
                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                MsgEntity msgEntity = new MsgEntity()
                {
                    msg_titile = msg_title,
                    msg_content = msg_content,
                    msg_content2 = msg_content,
                    msg_isread = "未读",
                    creater_id = userEntity.user_id,
                    receiver_ids = receiver_ids,
                    msg_lev = 1,
                    msg_sendtime = DateTime.Now.ToString(),
                    msg_appendix = "",
                    msg_appendix_name = "",
                    msg_save = 0,
                };
                return bs.AddMsg(msgEntity).ToString();

            }
            catch
            {
                return "ex";
            }
        }

        [WebMethod(Description = "Web 服务提供的方法，返回给定文件的字节数组(Base64String) |  如果返回为'false'则下载失败")]
        public string MsgDownLoadFile(string filename)
        {
            if (string.IsNullOrEmpty(filename)) return "false";

            try
            {
                string dirPath = Server.MapPath("/");
                dirPath = Path.Combine(dirPath, "webstore");
                dirPath = Path.Combine(dirPath, filename);
                if (!Directory.Exists(dirPath))
                    return "false";

                byte[] res = null;
                res = getBinaryFile(dirPath);
                return Convert.ToBase64String(res);
            }
            catch
            {
                return "false";
            }
        }

        #endregion

        #region 邮件
        private ClientManager manager = new ClientManager();

        [WebMethod(Description = "发送邮件 mailFrom 邮箱地址 userName 用户名 mailTo 接收邮件的邮箱地址 subject 主题 text 内容 password 邮箱密码 attachment 附件(附件名)")]
        public String MailSend(String mailFrom, String userName, String mailTo, String subject, String text, String password, String attachment)
        {
            //MailModel mailModel = new MailModel
            //{
            //    MailFrom = new System.Net.Mail.MailAddress(mailFrom, userName),
            //    MailTo = mailTo,
            //    Body = text,
            //    Subject = subject,
            //    Prority = MailPriority.Normal,
            //    Password = password
            //};

            //if (!String.IsNullOrEmpty(attachment))
            //{
            //    var root = Server.MapPath("~");
            //    root = Path.Combine(root, "webstore");
            //    root = Path.Combine(root, "Person");
            //    var paths = CommonMethod.splitString(attachment, ';');
            //    foreach (var item in paths)
            //    {
            //        var temp = item.Replace("/", "\\");
            //        var path = root + temp;
            //        mailModel.Attachments.Add(new Attachment(path));
            //    }
            //}

            try
            {
                var client = manager.GetSmtpClient(mailFrom, password, userName);
                var sendModel = new SendModel();
                sendModel.Subject = subject;
                sendModel.Body = text;
                var to = CommonMethod.splitString(mailTo);
                for (int i = 0; i < to.Length; i++)
                {
                    sendModel.AddTo(to[i]);
                }

                if (!String.IsNullOrEmpty(attachment))
                {
                    var root = Server.MapPath("~");
                    root = Path.Combine(root, "webstore");
                    root = Path.Combine(root, "Person");
                    var paths = CommonMethod.splitString(attachment, ';');
                    foreach (var item in paths)
                    {
                        var temp = item.Replace("/", "\\");
                        var path = root + temp;
                        sendModel.AddAttachmend(path);
                    }
                }
                client.SendMail(sendModel);
                client.Dispose();

            }
            catch
            {
                return "false";
            }

            return "true";
        }

        #endregion

        #region 网盘

        String webStoreBaseUrl = Path.Combine(ComVariable.StoreUrl, "webstore");

        [WebMethod(Description = "我的文件夹列表 返回格式：文件夹编码@文件夹名称,文件夹编码@文件夹名称")]
        public string GetMyFolderlist(string loginid)
        {
            if (string.IsNullOrEmpty(loginid)) return "false";

            try
            {
                StringBuilder res = new StringBuilder();
                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                if (userEntity == null) return "false";

                var folders = bs.FolderQueryAllByUserId(userEntity.user_id);
                if (folders == null || folders.Rows.Count == 0) return string.Empty;
                for (int i = 0; i < folders.Rows.Count; i++)
                {
                    res.Append(folders.Rows[i]["编码"] + "@");
                    res.Append(folders.Rows[i]["名称"] + " ,");
                }

                return res.ToString();
            }
            catch
            {
                return "false";
            }
        }

        [WebMethod(Description = "我的文件列表 返回格式：文件编码@文件名称@共享标志@文件大小@文件类型@文件夹编码,文件编码@文件名称@共享标志@文件大小@文件类型@文件夹编码§")]
        public string GetMyFilelist(string loginid, string keyword, int firstRecorder, int rowLength)
        {
            if (string.IsNullOrEmpty(loginid)) return "false";
            try
            {
                StringBuilder res = new StringBuilder();
                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                if (userEntity == null) return "false";

                var files = bs.FileQueryAllByUserId(userEntity.user_id);
                if (files == null || files.Rows.Count == 0 || firstRecorder > files.Rows.Count) return "";
                var res_files = files.AsEnumerable().Where(x => x["文件名称"].ToString().Contains(keyword)).CopyToDataTable();

                res_files = res_files.AsEnumerable().Skip(firstRecorder).Take(rowLength).CopyToDataTable<DataRow>();
                res_files.DefaultView.Sort = "更新时间 desc";

                for (int i = 0; i < res_files.Rows.Count; i++)
                {
                    res.Append(res_files.Rows[i]["文件编码"] + "@");
                    res.Append(res_files.Rows[i]["文件名称"] + "@");
                    res.Append(res_files.Rows[i]["共享标志"] + "@");
                    res.Append(res_files.Rows[i]["文件大小"] + "@");
                    res.Append(res_files.Rows[i]["文件类型"] + "@");
                    res.Append(res_files.Rows[i]["文件夹编码"] + " §");
                }

                return res.ToString();
            }
            catch
            {
                return "false";
            }
        }

        [WebMethod(Description = "共享文件列表 返回格式：文件编码@文件名称@共享人编码@共享人名称@文件大小@文件类型,文件编码@文件名称@共享人编码@共享人名称@文件大小@文件类型")]
        public string GetMySharedFilelist(string user_id)
        {
            try
            {
                StringBuilder res = new StringBuilder();

                var files = bs.FileQueryAllShared();
                if (files == null || files.Rows.Count == 0) return string.Empty;
                for (int i = 0; i < files.Rows.Count; i++)
                {
                    res.Append(files.Rows[i]["文件编码"] + "@");
                    res.Append(files.Rows[i]["文件名称"] + "@");
                    res.Append(files.Rows[i]["用户编码"] + "@");
                    res.Append(files.Rows[i]["用户名称"] + "@");
                    res.Append(files.Rows[i]["文件大小"] + "@");
                    res.Append(files.Rows[i]["文件类型"] + " ,");
                }

                return res.ToString();
            }
            catch
            {
                return "false";
            }
        }

        //String baseUrl = Path.Combine(ComVariable.StoreUrl, "webstore");
        //[WebMethod(Description = "文件夹删除 [forceflag:强制删除标志，如果为false且文件夹下面有文件，则删除失败]")]
        //public bool FolderDelete(string folderid, bool forceflag)
        //{
        //    try
        //    {
        //        bs.StoreDeleteFolder(folderid, baseUrl, forceflag);
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}

        //[WebMethod(Description = "文件夹重命名")]
        //public bool FolderRename(string loginid, string folderid, string folderpid, string rename)
        //{
        //    UserEntity userEntity = bs.QueryUserByLoginid(loginid);
        //    if (userEntity == null) return false;
        //    try
        //    {
        //        return bs.StoreRenameFolder(folderid, folderpid, rename, userEntity.user_id.ToString());
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}

        //[WebMethod(Description = "文件重命名")]
        //public bool FileRename(string fileid, string rename)
        //{
        //    try
        //    {
        //        bs.StoreRenameFile(fileid, rename);
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}

        //[WebMethod(Description = "文件删除")]
        //public bool FileDelete(string fileid)
        //{
        //    return true;
        //}

        [WebMethod(Description = "共享文件/取消共享 [shareflag:'1,2,3,6,'] 取消传string.empty ")]
        public bool FileShare(string fileid, string shareflag)
        {
            try
            {
                bs.FileShare(fileid, shareflag);
                return true;
            }
            catch
            {
                return false;
            }

        }



        #region 网盘文件上传

        [WebMethod(Description = "例 fileName:'a.jpg' folder_id:空则存到根文件夹目录下")]
        public bool SaveFile(byte[] bytes, int i, int length, string hasVlaue, string fileName, int allength, string user_id, string folder_id)//,string size,string format,string lastWriteTime,string macValue, 
        {
            if (string.IsNullOrEmpty(user_id) || !fileName.Contains('.'))
            {
                return false;
            }


            if (string.IsNullOrEmpty(folder_id)) folder_id = "0";

            //网盘文件夹存在验证
            if (!File.Exists(webStoreBaseUrl))
            {
                File.Create(webStoreBaseUrl);
            }

            string saveFileFullName = Path.Combine(webStoreBaseUrl, hasVlaue);

            bool b = false;
            if (length == bytes.Length)
            {
                string file_replacename = hasVlaue;
                using (FileStream fs = new FileStream(saveFileFullName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    fs.Position = fs.Length;
                    fs.Write(bytes, 0, bytes.Length);
                    if (fs.Length == allength)
                    {
                        b = true;
                    }

                    //如果已完成上传，则数据库中插入对应记录
                    if (b)
                    {
                        FileInfo fi = new FileInfo(saveFileFullName);
                        //string hashfilename = fs.GetFileHashName();
                        //string newsaveFileFullName = Path.Combine(webStoreBaseUrl, + hashfilename);
                        //fi.MoveTo(newsaveFileFullName);
                        string fileExt = Path.GetExtension(fileName);
                        FileEntity fe = new FileEntity()
                        {
                            file_id = -1,
                            file_name = fileName,
                            file_replacename = file_replacename,
                            file_ctime = DateUtil.getStandardTimeBySecond(),
                            file_descn = string.Empty,
                            file_ext = fileExt,
                            file_mime = OAclient.Common.FileHelper.GetFileMIME(fileExt),
                            file_path = file_replacename,
                            file_size = (allength / 1024).ToString("f2"),
                            user_id = Int32.Parse(user_id),
                            file_catalog = "",
                            file_share = "",
                            file_utime = DateUtil.getStandardTimeBySecond(),
                            folderID = folder_id
                        };

                        bs.StoreAddFile(fe);
                    }
                }
            }
            return b;
        }

        [WebMethod(Description = "我会告诉你这个是看断点的")]
        public long GetNowLength(string hasVlaue)//,string size,string format,string lastWriteTime,string macValue)
        {
            long i = 0;
            string newsaveFileFullName = Path.Combine(webStoreBaseUrl, hasVlaue);
            using (FileStream fs = new FileStream(newsaveFileFullName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                i = fs.Length;
            return i;
        }

        #endregion

        #region 网盘文件下载

        [WebMethod(Description = "Web 服务提供的方法，返回给定文件的字节数组(Base64String) |  如果返回'false'，则下载失败")]
        public string DownLoadFile(string loginid, string fileid, string filename)
        {
            if (string.IsNullOrEmpty(loginid) || string.IsNullOrEmpty(fileid) || string.IsNullOrEmpty(filename)) return "false";
            try
            {
                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                string dirPath = Server.MapPath("/");
                dirPath = Path.Combine(dirPath, "webstore");
                dirPath = Path.Combine(dirPath, "Person");
                dirPath = Path.Combine(dirPath, userEntity.user_id.ToString());
                if (!Directory.Exists(dirPath))
                    Directory.CreateDirectory(dirPath);

                var filelist = FileHelper.GetFileList(dirPath);
                string fullname = filelist[fileid];
                if (System.IO.Path.GetFileName(fullname) != filename)
                    return "";

                byte[] res = null;
                res = getBinaryFile(fullname);
                return Convert.ToBase64String(res);
            }
            catch
            {
                return "false";
            }
        }

        public byte[] getBinaryFile(string filename)
        {
            if (File.Exists(filename))
            {
                try
                {
                    ///打开现有文件以进行读取。
                    FileStream s = File.OpenRead(filename);
                    var res = ConvertStreamToByteBuffer(s);
                    s.Close();
                    return res;
                }
                catch (Exception e)
                {
                    return new byte[0];
                }
            }
            return new byte[0];
        }

        public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
        {
            int b1; System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
            while ((b1 = theStream.ReadByte()) != -1)
            {
                tempStream.WriteByte(((byte)b1));
            }
            var res = tempStream.ToArray();
            tempStream.Close();
            return res;
        }

        #endregion
        #endregion

        #region 公告

        PlanService ps = new PlanService();
        [WebMethod(Description = "获取公告列表 已读状态 0：未读；1：已读 返回（时间倒序）：'编码,标题ψ创建时间ψ内容ψ已读状态§'  ")]
        public string NotifyList(string loginid, int firstRecorder, int rowLength)
        {
            try
            {
                //var dt = ps.QueryAllNotify();
                //if (dt == null && dt.Rows.Count == 0 || firstRecorder > dt.Rows.Count) return "";
                //dt = dt.AsEnumerable().Skip(firstRecorder).Take(rowLength).CopyToDataTable<DataRow>();
                //var res = new StringBuilder();
                //if (dt != null && dt.Rows.Count > 0)
                //{
                //    for (int i = 0; i < dt.Rows.Count; i++)
                //    {
                //        res.Append(dt.Rows[i]["标题"] + "ψ");
                //        res.Append(dt.Rows[i]["创建时间"] + "ψ");
                //        res.Append(dt.Rows[i]["内容2"] + " §");
                //    }
                //}
                //return res.ToString();
                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                if (userEntity == null) return "false";

                var dt = bs.QueryReceveMsgByCondition(userEntity.user_id, "", "", "", "", 0, null);
                if (dt == null && dt.Rows.Count == 0 || firstRecorder > dt.Rows.Count) return "";

                dt.DefaultView.Sort = "发送时间 desc";
                DataTable data = dt.DefaultView.ToTable();
                if (data == null || data.Rows.Count == 0 || firstRecorder > data.Rows.Count) return "";

                data = data.AsEnumerable().Skip(firstRecorder).Take(rowLength).CopyToDataTable<DataRow>();
                var tmp_drs = data.Select("标题 like '企业公告%'");

                if (tmp_drs.Length > 0)
                    data = tmp_drs.CopyToDataTable();
                else
                    data = data.Clone();


                var res = new StringBuilder();
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        res.Append(dt.Rows[i]["标题"] + "ψ");
                        res.Append(dt.Rows[i]["编码"] + "ψ");
                        res.Append(dt.Rows[i]["发送时间"] + "ψ");
                        res.Append(dt.Rows[i]["内容2"] + "ψ");
                        res.Append(dt.Rows[i]["已读"] + " §");
                    }
                }
                return res.ToString();
            }
            catch
            {
                return "false";
            }
        }

        #endregion

        #region 外出登记

        [WebMethod(Description = "获取用户的登记记录 type:0(未确认) 1(已确认) 返回格式：登记编码ψ用户编码ψ登记日期ψ外出地点ψ外出事由ψ联系人ψ联系人电话ψ出门时间ψ预计返回时间ψ实际返回时间ψ备注§")]
        public string WcdjList(string loginid, string type, string keyword, int firstRecorder, int rowLength)
        {
            try
            {
                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                if (userEntity == null) return "false";
                DataTable wcdj_list = null;
                if (type == "0")
                    wcdj_list = bs.QueryWcdjById(userEntity.user_id, false);
                else
                    wcdj_list = bs.QueryWcdjById(userEntity.user_id, true);

                if (wcdj_list == null) return string.Empty;




                if (!string.IsNullOrEmpty(keyword))
                {
                    wcdj_list = wcdj_list.AsEnumerable().Where(x => x["登记号"].ToString().Contains(keyword) ||
                                                                             x["登记人编号"].ToString().Contains(keyword) ||
                                                                             x["登记日期"].ToString().Contains(keyword) ||
                                                                             x["外出地点"].ToString().Contains(keyword) ||
                                                                             x["外出事由"].ToString().Contains(keyword) ||
                                                                             x["联系人"].ToString().Contains(keyword) ||
                                                                             x["电话"].ToString().Contains(keyword) ||
                                                                             x["出门时间"].ToString().Contains(keyword) ||
                                                                             x["实际返回时间"].ToString().Contains(keyword) ||
                                                                             x["备注"].ToString().Contains(keyword)).ToArray().CopyToDataTable();
                }

                if (wcdj_list == null && wcdj_list.Rows.Count == 0 || firstRecorder > wcdj_list.Rows.Count) return "";
                wcdj_list = wcdj_list.AsEnumerable().Skip(firstRecorder).Take(rowLength).CopyToDataTable<DataRow>();
                StringBuilder res = new StringBuilder();
                for (int i = 0; i < wcdj_list.Rows.Count; i++)
                {
                    res.Append(wcdj_list.Rows[i]["登记号"] + "ψ");
                    res.Append(wcdj_list.Rows[i]["登记人编号"] + "ψ");
                    res.Append(wcdj_list.Rows[i]["登记日期"] + "ψ");
                    res.Append(wcdj_list.Rows[i]["外出地点"] + "ψ");
                    res.Append(wcdj_list.Rows[i]["外出事由"] + "ψ");
                    res.Append(wcdj_list.Rows[i]["联系人"] + "ψ");
                    res.Append(wcdj_list.Rows[i]["电话"] + "ψ");
                    res.Append(wcdj_list.Rows[i]["出门时间"] + "ψ");
                    res.Append(wcdj_list.Rows[i]["预计返回时间"] + "ψ");
                    res.Append(wcdj_list.Rows[i]["实际返回时间"] + "ψ");
                    res.Append(wcdj_list.Rows[i]["备注"] + " §");
                }
                return res.ToString();
            }
            catch (Exception e)
            {
                return "false";
            }
        }

        [WebMethod(Description = "loginid:当前用户账号 wcdj_addr :外出地点 wcdj_reason:原因 wcdj_lxr:联系人 wcdj_dh:联系人电话  wcdj_stime:外出开始时间 wcdj_pretime:预期回来时间 wcdj_mark:备注")]
        public bool WcdjAdd(string loginid, string wcdj_addr, string wcdj_reason, string wcdj_lxr, string wcdj_dh,
            string wcdj_stime, string wcdj_pretime, string wcdj_mark)
        {
            try
            {
                UserEntity userEntity = bs.QueryUserByLoginid(loginid);
                if (userEntity == null) return false;
                WcdjEntity wcdjEntity = new WcdjEntity();
                wcdjEntity.user_id = userEntity.user_id;
                wcdjEntity.wcdj_date = DateTime.Now.ToString("yyyy-MM-dd");
                wcdjEntity.wcdj_addr = wcdj_addr;
                wcdjEntity.wcdj_reason = wcdj_reason;
                wcdjEntity.wcdj_lxr = wcdj_lxr;
                wcdjEntity.wcdj_dh = wcdj_dh;
                wcdjEntity.wcdj_stime = wcdj_stime;
                wcdjEntity.wcdj_pretime = wcdj_pretime;
                wcdjEntity.wcdj_mark = wcdj_mark;
                bs.AddNewRegister(wcdjEntity);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        [WebMethod(Description = "外出登记返回时调用 wcdj_id:外出编码")]
        public bool WcdjReturn(string wcdj_id)
        {
            try
            {
                var wcdj_etime = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                bs.UpdateEtime(wcdj_id, wcdj_etime);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        [WebMethod(Description = "外出登记返回时调用 wcdj_id:外出编码")]
        public bool WcdjDelete(string wcdj_id)
        {
            try
            {
                var WcdjEntity = bs.WcdjQueryEntity(int.Parse(wcdj_id));

                if (DateTime.Parse(WcdjEntity.wcdj_date).ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd"))//确定是当天增加的记录
                {
                    //var wcdj_etime = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                    bs.DelectById(wcdj_id);
                    return true;
                }
                return false;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        #endregion


    }
}
